<?php

/**
 * Description of o2orecommend
 *
 * @author libaoling
 * @time 2014-12-18
 */
class O2orecommendApp extends BackendApp
{
    private $_o2orecommend_mod;

    function __construct()
    {
        $this->O2orecommendApp();
    }

    function O2orecommendApp()
    {
        parent::BackendApp();

        $this->_o2orecommend_mod =& m('o2orecommend');
    }

    function index()
    {
        $condition = "1";
        $recom_id = empty($_GET['recom_id']) ? 0 : intval($_GET['recom_id']);

        /* 取得推荐类型 */
        $recommend_conf = ROOT_PATH . "/wl_admin/includes/config/o2o.recommend.php";
        $recommends = file_exists($recommend_conf) ? require $recommend_conf : array();
        $this->assign('recommends', $recommends);

        /* 取得推荐商品 */
        $page = $this->_get_page();
        $storeo2o_mod =& m('storeo2o');
        $recom_id && $condition .= " AND ore.recom_id = '$recom_id'";
        $_GET['store_no'] && $condition .= " AND so.store_no like '%{$_GET['store_no']}%'";
        $_GET['store_name'] && $condition .= " AND so.store_name_search like '%{$_GET['store_name']}%'";
        
        $limit = " limit " . $page['limit'];
        $order = " order by ore.sort DESC, ore.id DESC ";
        $fields = " ore.id, so.store_id, so.store_no, so.store_name_search, so.store_floor, so.products_search, ore.sort recom_sort";
        $tables = " wl_store_o2o so right join wl_o2o_recommend ore on ore.store_id=so.store_id ";
        $sql = "select {$fields} from {$tables} where {$condition}{$order}{$limit}";
        $storeo2o_list = $storeo2o_mod->db->getAll($sql);
        
        $this->assign('storeo2o_list', $this->_remake_stores($storeo2o_list));
        
        $page['item_count'] = $storeo2o_mod->db->getOne("SELECT COUNT(*) FROM {$tables} where {$condition}");
        $this->_format_page($page);
        $this->assign('page_info', $page);

        $this->import_resource(array('script' => 'inline_edit.js'));
        $this->display('o2orecommend.store.html');
    }

    /* 取消推荐 */
    function drop_store_from()
    {
        if (empty($_GET['id']))
        {
            $this->show_warning('请先选择推荐位置');
            return;
        }
        $ids = explode(',', $_GET['id']);
    
        $this->admin_log("导购取消推荐".':'.$_GET['id']);
        
        //删除热卖、特色图片
        $this->_del_store_image($ids);
        
        $this->_o2orecommend_mod->drop($ids);
        $this->show_message('取消推荐成功');
    }

    // 异步修改数据
    function ajax_col()
    {
        $id     = $_GET['id'];
        $column = empty($_GET['column']) ? '' : trim($_GET['column']);
        $value  = intval($_GET['value']);
        $data   = array();
        $arr    = explode('-', $id);
        $id = intval($arr[0]);
        $store_id = intval($arr[1]);

        if (in_array($column ,array('sort')))
        {
            $data[$column] = $value;
            $this->_o2orecommend_mod->edit($id, $data);
            if(!$this->_o2orecommend_mod->has_error())
            {
                echo ecm_json_encode(true);
            }
        }
        else
        {
            return ;
        }
        return ;
    }
    
    private function _del_store_image($ids)
    {
        $storeo2o = m("storeo2o");
        $infos = $this->_o2orecommend_mod->find(db_create_in($ids, "id"));
        $storeInfo = array();
        foreach ($infos as $info)
        {
            $storeInfo[$info['recom_id']][] = $info['store_id'];
        }
        foreach ($storeInfo as $recom_id => $store_ids)
        {
            $field = "";
            if($recom_id == 1){
                $field = "feature_image";
            }
            if($recom_id == 2){
                $field = "hot_image";
            }
            $field && $storeo2o->edit($store_ids, array($field => ''));
        }
    }
    
    private function _remake_stores($stores)
    {
        $result = $stores;
        foreach ($stores as $key => $store)
        {
            if($store['store_name_search']){
                $store_names = explode('|', $store['store_name_search']);
                $result[$key]['store_name'] = $store_names[0];
            }
            if($store['products_search']){
                $productses = explode('|', $store['products_search']);
                $result[$key]['products'] = $productses[0];
            }
        }
        
        return $result;
    }
}
